require(['./config'],() => {
   require(['header','template','footer'],(header,template) => {
      class Search{
         constructor(){
            this.page = 1
            this.sort = 0
            this.init()
            this.getWord().then(() => {
               this.nextPrev()
               this.menu()
            })
         }
         init(){
            localStorage.removeItem('id')
         }
         getWord(){
            return new Promise(resolve => {
               const word = location.search.slice(6)
               console.log(decodeURIComponent(word))
               $.get(`/v1/search/result?keyword=${word}`,{
                  page:this.page,
                  size:20,
                  sort:this.sort
               },resp=>{
                  console.log(resp)
                  if(!resp.data.total){
                     $('.noSearch').css('display','flex')
                     $('.noSearch h2 b').html(decodeURIComponent(word))
                  }
                  $.get(`/product/spus?ids=${resp.data.spu}`,resp => {
                     console.log(resp)
                     const{list} = resp.data
                     $('#goods').html(template('goodsTemplate',{list}))
                
                     resolve()

                  })
               })
            })
           
         }
         nextPrev(){
            //下一页
           $('#next').on('click',() => {
              ++this.page
              // console.log(this.pageCount)
              if(this.page == this.pageCount){
                 alert('已经是最后一页咯')
                 $('#next').addClass('noClick')
                 
              }else{
                 this.getWord()
                 $('#prev').removeClass('noClick')
              }
              // this.getList()
           })
           //上一页
           $('#prev').on('click',() => {
              --this.page
              if(this.page <0){
                 alert('已经是最前面一页咯')
              $('#prev').addClass('noClick')
              }else{
                 this.getWord()
                 $('#next').removeClass('noClick')
              }
           })
           if(this.page == 1){
              $('#prev').addClass('noClick')
              // $('#prev').off('click')
           }
      
        }

        //综合排序
        menu(){
         const _this = this
         $('#sort').addClass('click-a')
         //价格低到高
         $('#price_low').on('click',function(){
            _this.sort = 1
            // console.log(this.sort)
            _this.getWord()
            $(this).addClass('click-a').siblings().removeClass('click-a')
         })
         //价格高到低
         $('#price_high').on('click',function(){
            _this.sort = 2
            // console.log(this.sort)
            _this.getWord()
            $(this).addClass('click-a').siblings().removeClass('click-a')
         })
         //综合排序
         $('#sort').on('click',function(){
            _this.sort = 0
            // console.log(this.sort)
            _this.getWord()
            $(this).addClass('click-a').siblings().removeClass('click-a')
         })
      }
      
      }
      new Search()
   })
})